﻿var gadgetWindow = getGadgetWindow();

function getGadgetWindow() {
    return System.Gadget.document.parentWindow;
}

function getFlag(code) {
    var flagFile = gadgetWindow.interop.CountryCodeToIso3166(code) + ".png";
    var name = gadgetWindow.interop.CountryCodeToName(code);
    return "<img src='images/flags/" + flagFile + "' alt='" + name + "' />";
}

function addPlayerSkillColumn(row, skill) {
    var skillName = gadgetWindow.interop.SkillValueToName(skill);
    addPlayerColumn(row, skill, skillName);
}

function addPlayerScoreColumn(row, score, maxScore) {
    var cell = addPlayerColumn(row, truncate(score, 0), truncate(score, 2));

    if (score == maxScore)
        $(cell).addClass("defaultPosition");
}

function addPlayerColumn(row, content, title) {
    var cell = row.insertCell(-1);
    cell.innerHTML = content;
    cell.title = title;
    return cell;
}

function addPlayerSkills(row, player) {
    addPlayerColumn(row, player.Name, "");
    addPlayerColumn(row, truncate(player.Age.YearsAsFraction(), 1), player.Age);
    addPlayerColumn(row, getFlag(player.Birthplace), "");
    addPlayerSkillColumn(row, player.Stamina);
    addPlayerSkillColumn(row, player.Form);
    addPlayerSkillColumn(row, player.Scoring);
    addPlayerSkillColumn(row, player.Playmaking);
    addPlayerSkillColumn(row, player.Winger);
    addPlayerSkillColumn(row, player.Passing);
    addPlayerSkillColumn(row, player.Defending);
    addPlayerSkillColumn(row, player.Keeper);
    addPlayerSkillColumn(row, player.SetPieces);
    addPlayerSkillColumn(row, player.Experience);
    addPlayerSkillColumn(row, player.Leadership);
    addPlayerColumn(row, player.TSI, "");
    addPlayerColumn(row, player.Specialty, "");

    //addStatusColumn(row, player.Cards, player.InjuryLevel);
    row.insertCell(-1).innerHTML = player.Cards; // TODO: combine with injury to one column called status
    row.insertCell(-1).innerHTML = player.InjuryLevel;
}

function addPlayerScores(row, player) {
    var playerScores = gadgetWindow.interop.GetPlayerScores(player);
    var deafultPositionScore = playerScores.GetPositionScore(playerScores.GetDefaultPosition());
    addPlayerScoreColumn(row, playerScores.ForwardScore, deafultPositionScore);
    addPlayerScoreColumn(row, playerScores.WingerScore, deafultPositionScore);
    addPlayerScoreColumn(row, playerScores.PlaymakerScore, deafultPositionScore);
    addPlayerScoreColumn(row, playerScores.WingbackScore, deafultPositionScore);
    addPlayerScoreColumn(row, playerScores.DefenderScore, deafultPositionScore);
    addPlayerScoreColumn(row, playerScores.GoalkeeperScore, deafultPositionScore);
    addPlayerScoreColumn(row, playerScores.PenaltyShooterScore, deafultPositionScore);
    addPlayerScoreColumn(row, playerScores.CaptainScore, deafultPositionScore);
}

function addPlayer(index, player) {
    var table = document.getElementById("playerGrid").tBodies[0];
    var row = table.insertRow(-1);

    addPlayerSkills(row, player);
    addPlayerScores(row, player);

    if (player.InjuryLevel >= 0)
        $(row).addClass("injured");

    // add more? "extra but hidden" columns like stats best match rating last match rating etc
}

function truncate(number, digits) {
    return Math.floor(number * Math.pow(10, digits)) / Math.pow(10, digits);
}

function updatePlayerGrid() {
    var players = gadgetWindow.interop.ToArray(gadgetWindow.MyTeam.Players);
    $.each(players, addPlayer);
}

function enableSorting() {
    $("#playerGrid").tablesorter({ widgets: ['zebra'] });
}

function showError(msg) {
    $("body").prepend("<span>" + msg + "</span>").css("color", "red");
}

function updateGeneralInfo() {

    var team = gadgetWindow.MyTeam;
    $("#teamName").text(team.Name);
    $("#teamCountryFlag").replaceWith(getFlag(team.Country.ID));
    $("#regionName").text(team.Region.Name);
    $("#arenaName").text(team.Arena);
    $("#leaguePosition").text(team.LeagueStats.Position);
    $("#leagueUnitLevelName").text(team.League.LevelUnitName);

    if (!team.StillInCup)
        $("#cupStatus").text("Still in Cup");
}

$(document).ready(function() {

    if (!gadgetWindow) {
        showError("Missing something...");
        return;
    }

    updateGeneralInfo();
    updatePlayerGrid();
    enableSorting();
});
